<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules" label-width="140" label-position="top">
			<uni-forms-item required label="门店名称" name="name">
				<uni-easyinput type="text" v-model="formData.name" placeholder=" 请输入 门店名称" />
			</uni-forms-item>
			<uni-forms-item required label="wifi名称" name="wifiname">
				<!-- 		<input  class="input" v-model="formData.wifiname" type="text" placeholder="wifi名称" />
				<uni-icons @click="iconClick"  type="search" size="30"></uni-icons> -->
				<uni-easyinput trim="all" suffixIcon="search" v-model="formData.wifiname" type="text"
					placeholder="请输入 WiFi 名称" @iconClick="iconClick"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item required label="wifi密码" name="wifipwd">
				<uni-easyinput class="input" v-model="formData.wifipwd" type="text" placeholder="请输入 wifi密码" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submit" >{{submitStatus ? "创建Wi-Fi": "修改Wi-Fi"}}</button>
	</view>
</template>





<script>
	var http = require("../../utils/http.js");
	export default {
		data() {
			return {
				// 表单数据
				formData: {
					id: '',
					name: '',
					wifiname: '',
					wifipwd: '',
					wifiList: []

				},
				submitStatus: true,
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入门店名称',
						}]
					},
					// 对email字段进行必填验证
					wifiname: {
						rules: [{
							required: true,
							errorMessage: '请输入wifi名称',
						}]
					},
					wifipwd: {
						rules: [{
							required: true,
							errorMessage: '请输入wifi密码',
						}]
					}
				}
			}
		},
		onLoad(res) {
			var that=this
			if(Object.keys(res).length!=0){
				// console.log(res)
				var wifi = JSON.parse(decodeURIComponent(res.params));
				this.$data.formData.id = wifi.id
				this.$data.formData.name = wifi.storeName
				this.$data.formData.wifiname = wifi.wifiName
				this.$data.formData.wifipwd = wifi.wifiPwd
				that.submitStatus=false
			}
			

			
		},
		methods: {

			/**
			 * 复写 binddata 方法，如果只是为了校验，无复杂自定义操作，可忽略此方法
			 * @param {String} name 字段名称
			 * @param {String} value 表单域的值
			 */
			// binddata(name,value){
			// 通过 input 事件设置表单指定 name 的值
			//   this.$refs.form.setValue(name, value)
			// },
			// 触发提交表单
			submit() {
				this.$refs.form.validate().then(res => {
					var params = {
						url: "/p/wifi/save",
						method: "POST",
						data: {
							id: this.$data.formData.id,
							userId: uni.getStorageSync("userId"),
							storeName: res.name,
							wifiName: res.wifiname,
							wifiPwd: res.wifipwd
						},
						callBack: res => {
							this.$data.formData.id = ""
							this.$data.formData.name = ""
							this.$data.formData.wifiname = ""
							this.$data.formData.wifipwd = ""
							uni.showToast({
							  icon: "none",
							  title: this.$data.submitStatus ? "创建Wi-Fi成功！": "修改Wi-Fi成功！"
							});
							this.$data.submitStatus=true
							
							uni.showModal({
								title: this.$data.submitStatus ? "创建Wi-Fi成功！": "修改Wi-Fi成功！",
								content: '是否继续创建',
								success: function (res) {
									if (res.confirm) {
										
									} else if (res.cancel) {
										uni.navigateTo({
											url: "/subPackpages/wifi-list/wifi-list"
										})
									}
								}
							});
							

						}

					};
					http.request(params);

					//去保存顺便生成二维码
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			iconClick() {
				var that = this;
				uni.startWifi({
					success: (res) => {
						// 成功了的话获取当前已连接的wifi，如果没有的话则显示附近的wifi列表
						uni.getConnectedWifi({
							partialInfo: true,
							success(result) {
								that.$data.formData.wifiname = result.wifi.SSID || ""
								// formData.wifi_name = result.wifi.SSID || ""
								// console.log("wifi", result)
							},
							fail(err) {

								// 	uni.getSetting({
								// 		success(res) {
								// 			if (!res.authSetting['scope.userLocation']) {
								// 				uni.authorize({
								// 					scope: 'scope.userLocation',
								// 					success() {
								// 						getWifiList()
								// 					},
								// 					fail(err) {
								// 						// console.log("未授权err", err);
								// 					}
								// 				})
								// 			} else {
								// 				getWifiList()
								// 			}
								// 		}
								// 	})


							}
						})
					},
					fail: (error) => {
						// console.log("初始化失败", error)
					}
				})

			},
			getWifiList() {
				uni.getWifiList({
					success(wifis) {
						// console.log(
						// 	"获取wifi成功",
						// 	wifis)
						uni.onGetWifiList(function(res) {
							console.log("获取wifi列表", res.wifiList);
							// const listFilterEmpty = res.wifiList.filter(item => item.SSID)
							// // const curSet = new Set(...listFilterEmpty.map(item=>item.SSID))
							// const resultList = []
							// wifiList.data = listFilterEmpty.reduce((prev, next) => {

							// 	const temp = prev.some((item) =>
							// 		item.SSID === next.SSID
							// 	)
							// 	if (!temp) {
							// 		return prev.concat(next)
							// 	} else {
							// 		return prev
							// 	}
							// }, [])
							// dataPicker.value.open()
						})
						// wifiList.data =
						// 	wifis
					},
					fail(result) {
						// console.log(
						// 	"获取wifi失败",
						// 	result)
					}
				})
			}



		}
	}
</script>

<style>

</style>